<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/base_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/account.php");

	class AccountMod extends BaseMod {
		public static function getAfmAccount($pilotId) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from account where pid = %d",
					$pilotId
				);
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row){
                            $sql =
                                    sprintf(
                                            "select * from account where id = %d",
                                            $pilotId
                                    );
                            $row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
                            if (!$row)
				return null;
                        }
			return
				new Account(
                                            $row["name"],
                                            $row["password"],
                                            $row["universe"],
                                            $row["permissions"],
                                            $row["level"],
                                            $row["validation"],
                                            $row["pid"],
                                            $row["basic_script_access"],
                                            $row["advanced_script_access"],
                                            $row["administrative_script_access"],
                                            $row["sb_script_access"],
                                            $row["secret_script_access"],
                                            $row["chat_level"]
				);
		}
                
		public static function getAccountByPid($pid) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from account where pid = %d",
					$pid
				);
			//echo $sql;
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row){
                            $sql =
                                    sprintf(
                                            "select * from account where id = %d",
                                            $pid
                                    );
                            $row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
                            if (!$row)
				return null;
                        }
			return
				new Account(
                                            $row["name"],
                                            $row["password"],
                                            $row["universe"],
                                            $row["permissions"],
                                            $row["level"],
                                            $row["validation"],
                                            $row["pid"],
                                            $row["basic_script_access"],
                                            $row["advanced_script_access"],
                                            $row["administrative_script_access"],
                                            $row["sb_script_access"],
                                            $row["secret_script_access"],
                                            $row["chat_level"]
				);
		}
                
		public static function getAccount($name) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from ".SettingsMod::DB_TABLE_PREFIX."account where name = '%s'",
					mysql_real_escape_string($name)
				);
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new Account(
                                            $row["name"],
                                            $row["password"],
                                            $row["universe"],
                                            $row["permissions"],
                                            $row["level"],
                                            $row["validation"],
                                            $row["pid"],
                                            $row["basic_script_access"],
                                            $row["advanced_script_access"],
                                            $row["administrative_script_access"],
                                            $row["sb_script_access"],
                                            $row["secret_script_access"],
                                            $row["chat_level"]
				);
		}
                
		public static function getChatAccount($name, $password) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from ".SettingsMod::DB_TABLE_PREFIX."account where name = '%s' and password = '%s'",
					mysql_real_escape_string($name),
                                        mysql_real_escape_string($password)
				);
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new Account(
                                            $row["name"],
                                            $row["password"],
                                            $row["universe"],
                                            $row["permissions"],
                                            $row["level"],
                                            $row["validation"],
                                            $row["pid"],
                                            $row["basic_script_access"],
                                            $row["advanced_script_access"],
                                            $row["administrative_script_access"],
                                            $row["sb_script_access"],
                                            $row["secret_script_access"],
                                            $row["chat_level"]
				);
		}

		public static function getAccounts(&$pageNumber, &$pageCount) {
			$conn = self::getConnection();

			$where = sprintf("where universe = '%s' ", $_SESSION["account"]->getUniverse());

			$sql = "select count(*) as cnt from ".SettingsMod::DB_TABLE_PREFIX."account " . $where;
			$result = mysql_query($sql, $conn);
			$row = mysql_fetch_assoc($result);
			$recordCount = $row["cnt"];
			$pageCount = ceil($recordCount / SettingsMod::PAGE_RECORDS_PER_PAGE);
			if ($pageNumber > $pageCount)
				$pageNumber = $pageCount;


			if ($pageNumber < $pageCount)
				$recordsPerPage = SettingsMod::PAGE_RECORDS_PER_PAGE;
			else {
				$recordsPerPage = $recordCount % SettingsMod::PAGE_RECORDS_PER_PAGE;
				if ($recordsPerPage == 0 && $recordCount > 0)
					$recordsPerPage = SettingsMod::PAGE_RECORDS_PER_PAGE; 
			}
			$sql =
				sprintf(
					"select * from ( " .
						"select * from (" .
							"select * from ".SettingsMod::DB_TABLE_PREFIX."account " .
							$where .
							"order by `name` asc " .
							"limit 0, %d" .
						") as tmp1 " .
						"order by `name` asc " .
						"limit %d, %d " .
					") as tmp2 " .
					"order by `name` asc",
					SettingsMod::PAGE_RECORDS_PER_PAGE * $pageNumber,
                                        SettingsMod::RECORDS_PER_PAGE * ($pageNumber - 1),
					$recordsPerPage
				);
			$accounts = array();
			$result = mysql_query($sql, $conn);
			while ($row = mysql_fetch_assoc($result)) {
				$acc =
					new Account(
                                                    $row["name"],
                                                    $row["password"],
                                                    $row["universe"],
                                                    $row["permissions"],
                                                    $row["level"],
                                                    $row["validation"],
                                                    $row["pid"],
                                                    $row["basic_script_access"],
                                                    $row["advanced_script_access"],
                                                    $row["administrative_script_access"],
                                                    $row["sb_script_access"],
                                                    $row["secret_script_access"],
                                                    $row["chat_level"]
					);
				$accounts[] = $acc;
			}
			return $accounts;
		}

		public static function updateLevel($name, $level) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update ".SettingsMod::DB_TABLE_PREFIX."account set level = '%s' where name = '%s'",
				mysql_real_escape_string($level),
				mysql_real_escape_string($name)
			);

			mysql_query($sql, $conn);
		}

		public static function updatePermissions($name, $permissions) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update ".SettingsMod::DB_TABLE_PREFIX."account set permissions = %d where name = '%s'",
				intval($permissions),
				mysql_real_escape_string($name)
			);

			mysql_query($sql, $conn);
		}

		public static function addAccount($acc) {
			$conn = self::getConnection();
			$sql = sprintf(
				"insert into ".SettingsMod::DB_TABLE_PREFIX."account (universe, name, password, permissions, level, validation, pid, basic_script_access, advanced_script_access, administrative_script_access, sb_script_access, secret_script_access, chat_level) values ('%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, %d, %d, %d, %d)",
				mysql_real_escape_string($acc->getUniverse()),
				mysql_real_escape_string($acc->getName()),
				mysql_real_escape_string($acc->getPassword()),
				intval($acc->getRawPermissions()),
				mysql_real_escape_string($acc->getLevel()),
                                mysql_real_escape_string($acc->getValidation()),
                                intval($acc->getPilotId()),
                                intval($acc->getBasic()),
                                intval($acc->getAdvanced()),
                                intval($acc->getAdmin()),
                                intval($acc->getSB()),
                                intval($acc->getSecret()),
                                intval($acc->getChatLevel())
			);
                        echo($sql);
			mysql_query($sql, $conn);
		}

		public static function deleteAccount($acc) {
			$conn = self::getConnection();
			$sql = sprintf(
				"delete from ".SettingsMod::DB_TABLE_PREFIX."account where name = '%s'",
				$acc->getName()
			);

			mysql_query($sql, $conn);
		}
		
		public static function updatePassword($name, $password) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update account set password = '%s' where name = '%s'",
				mysql_real_escape_string($password),
				mysql_real_escape_string($name)
			);

			mysql_query($sql, $conn);
		}
		
		public static function updatePid($name, $pid) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update account set pid = %d where name = '%s'",
				mysql_real_escape_string($pid),
				mysql_real_escape_string($name)
			);

			mysql_query($sql, $conn);
		}
                
		public static function updateValidation($name, $validation) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update account set validation = '%s' where name = '%s'",
				mysql_real_escape_string($validation),
				mysql_real_escape_string($name)
			);

			mysql_query($sql, $conn);
		}
                
                public static function updateAlertPermissions($name, $basic, $advanced, $admin, $pfcsb, $secret){
                        $conn = self::getConnection();
			$sql = sprintf(
				"update ".SettingsMod::DB_TABLE_PREFIX."account set basic_script_access = %d, advanced_script_access = %d, administrative_script_access = %d, sb_script_access = %d, secret_script_access = %d where name = '%s'",
				intval($basic),
                                intval($advanced),
                                intval($admin),
                                intval($pfcsb),
                                intval($secret),
				mysql_real_escape_string($name)
			);
                        
                        mysql_query($sql, $conn);
                }
                
                public function updateChatLevel($name, $level){
                        $conn = self::getConnection();
                        $sql = sprintf(
                                "update account set chat_level = %d where name = '%s'",
                                intval($level),
                                mysql_real_escape_string($name)
                                );
                        mysql_query($sql, $conn);
                }
                
                public static function getChatLevels(){
                    $conn = self::getConnection();
                    $levels = array();
                    $sql = "select * from ajax_chat_level order by id";
                    $result = mysql_query($sql, $conn);
                    while ($row = mysql_fetch_assoc($result)) {
                        $id = $row["id"];
                        $name = $row["name"];
                        $levels[$id]['Name'] = $name;
                    }
                    
                    return $levels;
                }
	}
?>